package com.im.partchat.ui.fragment.learn;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.im.partchat.App;
import com.im.partchat.R;
import com.im.partchat.adapter.base.XRecyclerViewAdapter;
import com.im.partchat.adapter.learn.NewTab6FragmentAdapter;
import com.im.partchat.base.BaseFragment;
import com.im.partchat.base.ILoadingType;
import com.im.partchat.bean.learn.Tab6New;
import com.im.partchat.bean.request.RequestBean;
import com.im.partchat.mvp.persenter.GetNewsPersenter;
import com.im.partchat.utils.Navigation;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by flyou on 2017/6/30.
 * VersionCode: 1
 * Desc:
 */

public class Tab6Fragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, XRecyclerViewAdapter.OnItemClickListener, XRecyclerViewAdapter.OnLoadMoreListener {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.reFreshLayout)
    SwipeRefreshLayout reFreshLayout;
    NewTab6FragmentAdapter adapter;
    private GetNewsPersenter getNewsPersenter;
    private int currentPageNo = 1;
    private static boolean isFirst = true;
    private List<Tab6New> tab1NewList = new ArrayList<>();

    @Override
    public void onLoadDataSuccess(Object o, String flag) {

        List<Tab6New> myTab1NewList ;
        Gson gson = new Gson();
        String json = gson.toJson(o);
        Type type = new TypeToken<List<Tab6New>>() {
        }.getType();
        myTab1NewList = gson.fromJson(json.toString(), type);
        Log.d(TAG, "onLoadDataSuccess: " + Thread.currentThread().getName());

        if (myTab1NewList != null) {
            if (myTab1NewList.size() == 10) {
                adapter.isLoadMore(true);
            } else {
                adapter.showLoadComplete();
            }
        }

        if (currentPageNo == 1) {
            if (myTab1NewList == null || myTab1NewList.size() == 0) {
                showEmpty();
                return;
            }


            adapter.clear();
            adapter.addAll(myTab1NewList);
        } else {
            if (myTab1NewList == null || myTab1NewList.size() == 0) {
                adapter.showLoadComplete();
                return;
            }
            if (myTab1NewList.size()<10){
                adapter.showLoadComplete();
            }
            adapter.addAll(myTab1NewList);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_learn_list;
    }

    @Override
    protected void initData() {
        getNewsPersenter = new GetNewsPersenter(this, this);
        getNewsPersenter.getNewList(RequestBean.COLUMN_QYFC, 1);
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        reFreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        reFreshLayout.setOnRefreshListener(this);

        adapter = new NewTab6FragmentAdapter(App.getApplication(), recyclerView, tab1NewList);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(App.getApplication(), LinearLayoutManager.VERTICAL, false));
        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this);
    }

    @Override
    protected ILoadingType.LoadingType initLoadType() {
        return ILoadingType.LoadingType.LOADING_TYPE_COVER;
    }

    @Override
    public void showLoading(String msg) {
        if (isFirst) {
            super.showLoading(msg);
        }
    }

    @Override
    public void hideLoading() {
        if (isFirst) {
            super.hideLoading();
            isFirst = false;
        }
        reFreshLayout.setRefreshing(false);
    }

    @Override
    public void onRefresh() {
        currentPageNo = 1;

        getNewsPersenter.getNewList(RequestBean.COLUMN_QYFC, 1);
    }

    @Override
    public void onItemClick(View v, int position) {
        Navigation.navigateWebview("新闻详情", tab1NewList.get(position).getUrl());
    }

    @Override
    public void onRetry() {

        getNewsPersenter.getNewList(RequestBean.COLUMN_QYFC, currentPageNo);
    }

    @Override
    public void onLoadMore() {
        currentPageNo++;
        getNewsPersenter.getNewList(RequestBean.COLUMN_QYFC, currentPageNo);
    }

}
